package sysmgr.controller;

import com.alibaba.fastjson.JSON;
import sysmgr.bean.*;
import sysmgr.service.AddressService;
import sysmgr.service.OrderService;
import sysmgr.service.impl.OrderServiceImpl;
import sysmgr.service.impl.StaffAddressServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Service;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class OrderController extends HttpServlet {

    /**
     * 实例化StaffDateService对象
     */
    private OrderService OrderService = new OrderServiceImpl();

    /**
     * 重写doGet方法
     *      不进行逻辑处理，直接调用doPost方法
     *
     * @param req 请求对象
     * @param resp 相应对象
     * @throws ServletException Servlet异常
     * @throws IOException IO流异常
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    /**
     * 重写doPost方法
     *
     * @param request 请求对象
     * @param response 相应对象
     * @throws ServletException Servlet异常
     * @throws IOException IO流异常
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端要传入的要调用哪个方法的名称
        String method = request.getParameter("method");
        //执行查询所有的方法
        if ("queryAll".equals(method)) {
            this.queryAll(request, response);
        }
        if ("deleteAll".equals(method)){
            this.deleteAll(request,response);
        }
        if("updateAll".equals(method)){
            this.updateAll(request,response);
        }

    }

    private void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //创建分页对象
        Page page = new Page();
        //前端要传入current(名称必须为current)当前页
        String currentString = request.getParameter("current");
        //每页显示的大小
        String sizeString = request.getParameter("size");
        //获取传入的用户ID，用于进行条件查询
            String staffId = request.getParameter("staffId");
            //创建书本信息实体类，用来存放接收到书本名称信息
            StaffOrder staffOrder = new StaffOrder();
            //前端获取到的书本名称封装到实体类中
        if (staffId!=null) {
            staffOrder.setStaffId(Integer.parseInt(staffId));
        }
        //创建Integer类型对象，用来接收转换后的数据，默认值是1和5
        Integer current = 1;
        Integer size = 5;
        //判断传入的是否为空，如果不为空就进行类型的转换Stirng --> Integer
        if ( sizeString != null) {
            size = Integer.parseInt(sizeString);
        }
        if (currentString != null) {
            current = Integer.parseInt(currentString);
        }
        //对传入的分页数据封装
        page.setCurrent(current);
        page.setSize(size);

        //获取查询到的数据的数据
        List<StaffOrder> staffDateList = OrderService.queryStaffDateAll(page, staffOrder);
        //将获取到的数据封装到响应数据对象中
        ResponseDate responseDate = new ResponseDate();
        responseDate.setMsg("操作成功");
        responseDate.setData(staffDateList);
        //将响应数据对象转换为json格式
        String json = JSON.toJSONString(responseDate);
        //通过响应对象获取打印输出流

        response.setContentType("application/json;charset=UTF-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.write(json);
    }
    private void deleteAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //创建分页对象
        Page page = new Page();
        //前端要传入current(名称必须为current)当前页
        String currentString = request.getParameter("current");
        //每页显示的大小
        String sizeString = request.getParameter("size");
        // 获取传入的用户ID，用于进行条件查询
        String OrderId = request.getParameter("id");
        //创建书本信息实体类，用来存放接收到书本名称信息
        StaffOrder staffOrder = new StaffOrder();
        //前端获取到的书本名称封装到实体类中
        if(OrderId != null) {
            staffOrder.setId(Integer.parseInt(OrderId));
        }

        //获取查询到的数据的数据
        String staffDateList = OrderService.deleteStaffDateAll(staffOrder);
        //将获取到的数据封装到响应数据对象中
        ResponseDate responseDate = new ResponseDate();
        responseDate.setMsg("操作成功");
        responseDate.setData(staffDateList);
        //将响应数据对象转换为json格式
        String json = JSON.toJSONString(responseDate);
        //通过响应对象获取打印输出流

        response.setContentType("application/json;charset=UTF-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.write(json);
    }
    public void updateAll(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        //创建分页对象
        Page page = new Page();
        //前端要传入current(名称必须为current)当前页
        String currentString = request.getParameter("current");
        //每页显示的大小
        String sizeString = request.getParameter("size");
        //获取传入的书本名称，用于进行条件删除
        String Status = request.getParameter("status");
        String Id = request.getParameter("id");
        //创建书本信息实体类，用来存放接收到书本名称信息
        StaffOrder staffOrder = new StaffOrder();
        //前端获取到的书本名称封装到实体类中
        if (Status!=null && Id!=null){
            staffOrder.setStatus(Integer.parseInt(Status));
            staffOrder.setId(Integer.parseInt(Id));
        }
        //获取查询到的数据的数据
        String staffDateList = OrderService.updateStaffDateAll(staffOrder);
        //将获取到的数据封装到响应数据对象中
        ResponseDate responseDate = new ResponseDate();
        responseDate.setMsg("操作成功");
        responseDate.setData(staffDateList);
        //将响应数据对象转换为json格式
        String json = JSON.toJSONString(responseDate);
        //通过响应对象获取打印输出流

        response.setContentType("application/json;charset=UTF-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.write(json);

    }
}


